//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.dstz.bpm.engine.action.handler.task;

import cn.hutool.core.bean.BeanUtil;
import com.dstz.base.api.exception.BusinessException;
import com.dstz.base.core.id.IdUtil;
import com.dstz.bpm.api.constant.ActionType;
import com.dstz.bpm.api.constant.TaskStatus;
import com.dstz.bpm.api.exception.BpmStatusCode;
import com.dstz.bpm.core.manager.BpmTaskOpinionManager;
import com.dstz.bpm.core.model.BpmTask;
import com.dstz.bpm.core.model.BpmTaskOpinion;
import com.dstz.bpm.engine.action.cmd.DefualtTaskActionCmd;
import com.dstz.sys.api.model.SysIdentity;
import com.dstz.sys.util.ContextUtil;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;

@Component
public class TaskTurnActionHandler extends AbstractTaskActionHandler<DefualtTaskActionCmd> {
   @Resource
   BpmTaskOpinionManager j;

   public TaskTurnActionHandler() {
   }

   @Override
   public ActionType getActionType() {
      return ActionType.TURN;
   }

   @Override
   public void execute(DefualtTaskActionCmd model) {
      this.c(model);
      this.l(model);
      BpmTask task = (BpmTask)model.getBpmTask();
      List<SysIdentity> userSetting = model.getBpmIdentity(task.getNodeId());
      if (BeanUtil.isEmpty(userSetting)) {
         throw new BusinessException(BpmStatusCode.NO_ASSIGN_USER);
      } else {
         task.setAssigneeId(((SysIdentity)userSetting.get(0)).getId());
         task.setAssigneeNames(((SysIdentity)userSetting.get(0)).getName());
         task.setStatus(TaskStatus.TURN.getKey());
         this.aJ.update(task);
         this.a(task, model.getOpinion());
      }
   }


   private void a(BpmTask task, String taskOpinion) {
      BpmTaskOpinion opinion = this.j.getByTaskId(task.getId());
      String opinionId = opinion.getId();
      String assignInfo = opinion.getAssignInfo();
      opinion.setId(IdUtil.getSuid());
      opinion.setAssignInfo(String.format("user-%s-%s,", task.getAssigneeNames(), task.getAssigneeId()));
      this.j.create(opinion);
      opinion.setId(opinionId);
      opinion.setAssignInfo(assignInfo);
      opinion.setTaskId("-");
      opinion.setOpinion(taskOpinion);
      opinion.setStatus(ActionType.TURN.getKey());
      opinion.setApproveTime(new Date());
      opinion.setApprover(ContextUtil.getCurrentUserId());
      opinion.setApproverName(ContextUtil.getCurrentUser().getFullname());
      opinion.setDurMs(opinion.getApproveTime().getTime() - opinion.getCreateTime().getTime());
      this.j.update(opinion);
   }


   public int getSn() {
      return 6;
   }

   public Boolean isDefault() {
      return true;
   }

   public String getConfigPage() {
      return "/bpm/task/taskTrunActionDialog.html";
   }
}
